package com.eascs.admin.o2o.submitted.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.submitted.entity.O2oLoanSubmittedEntity;
import com.eascs.admin.o2o.submitted.serviceimpl.O2oLoanSubmittedServiceImple;
import com.eascs.admin.o2o.submitted.vo.O2oLoanSubmittedVo;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.type.SystemModule;

@Controller
@RequestMapping("/submitted")
public class O2oLoanSubmittedController extends BaseMasterViewController {

	@Autowired
	O2oLoanSubmittedServiceImple o2oLoanSubmittedServiceImple;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private BaseUtils baseUtils;
	@Reference
	SysOrgService sysOrgService;
	@RequestMapping("/index")
	public String index(O2oLoanSubmittedVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "AJBS");
		vo.setPageSize(10);
		if (null == vo.getPageIndex()) {
			vo.setPageIndex(0);
		}
		String o2ourl = "/submitted/index";
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();
		String loanUserName = vo.getLoanUserName() == null ? "" : vo.getLoanUserName();
		String flowNumber = vo.getFlowNumber() == null ? "" : vo.getFlowNumber();
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "flowNumber", flowNumber);
		o2ourl = o2ourl + "&pageIndex=";
		List<User> users=sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no="";
		if (users!=null) {
			for (User user : users) {
				no+=user.getNo()+",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length()-1, no.length()))) {
				no=no.substring(0, no.length()-1);
			}
		}
		vo.setUsersno(no);
		String titles[] = { "业务单号", "贷款单号", "借款人姓名", "省区", "经办平台", "信贷经理", "放款金额", "贷款到期日", "流程状态", "创建时间" };
		SelectDataOption[] selectDataOptions = { null, null, null, null, null, null, null, null, new SelectDataOption("O2O_BILLSTATUS"), null };
		PageResult<Object[]> pageResult = o2oLoanSubmittedServiceImple.queryEntitys(vo);
		Button detail = ButtonHelper.createOpenButton(null, "/submitted/detail?id=");
		Button titleButtons[] = { detail };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setTitleButtons(titleButtons);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "/submitted/index";
	}

	@RequestMapping("/detail")
	public String detail(O2oLoanSubmittedVo vo) {
		O2oLoanSubmittedEntity o2oLoanSubmittedEntity = o2oLoanSubmittedServiceImple.getEntityById(vo.getId());
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(o2oLoanSubmittedEntity.getLoanappNo());
		O2oLoanUserEntity o2oLoanUserEntity = o2oLoanapplyService.findLoanUserEntity(o2oLoanapplyEntity.getLoanapplyNo(), o2oLoanapplyEntity.getLoanId());
		String agreeUrl = baseUtils.getApprovalAgreeUrl(vo.getId(), null, SystemModule.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("o2oLoanapplyEntity", o2oLoanapplyEntity);
		ViewContext.put("o2oLoanSubmittedEntity", o2oLoanSubmittedEntity);
		ViewContext.put("o2oLoanUserEntity", o2oLoanUserEntity);
		return "/submitted/detail";
	}

}
